package system;

public class TextHandler {

	int[] table;
	int index;
	Text[] letters;
	String inputText ="";
	String filename;
	int curDuration;
	int totalDuration;

	float x, y, xvel, yvel;
	int count;              // how many Text we have initialized
	int timeout;			// cycles the text should stay on-screen
	int splash;				// 0 = no splash effect, -1 = towards left, 1 = towards right
	int fontWidth;          // width of the font
	float alphaMul;			// an alpha transparency multiplier
	public boolean leftAlign;
	public boolean needsUpdate = false;
	public boolean isAlive = false;
	
	public TextHandler(int x, int y, String filename, int duration, int splash, boolean leftAlign, int width, int height){
		letters = new Text[10];
		for(int i = 0; i < 10; i++){
			try {
				letters[i] = new Text(36, x, y, filename, width, height);
				fontWidth = width;
				letters[i].x += fontWidth*i;
				

			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("letters[] initialization fail");
			}
		}
		table = new int[100];
		this.filename = filename;
		this.totalDuration = duration;
		this.curDuration = 0;
		this.count = 0;
		this.splash = splash; 			// if this is true we will do a cool arc anim
		this.leftAlign = leftAlign;
		if (splash !=0){
			xvel=(float) (3*splash + (Math.random()*4-2));
			yvel=(float) (-7 + (Math.random()*4-2));
		} else {
			xvel = yvel = 0;
		}
		index = 0;
		this.x = 0;
		this.y = 0;

		if(inputText != ""){
			if(inputText.length() > 10){
				inputText = inputText.substring(0, 10);
			}
			
			requireUpdate(true);
			update();
		}
		

	}
	public TextHandler(int x, int y, String filename, int duration, int splash, boolean leftAlign){
		this(x, y, filename, duration, splash, leftAlign, 7, 10);
	}

	public void update(){
		
		if(splash != 0){
			x += xvel;
			y += yvel;
		}
		
		if(totalDuration != -1){
			if(curDuration < totalDuration){
				curDuration++;
			}
			if(curDuration >= totalDuration){
				isAlive = false;
			}
		}

		for (int i = 0; i < 10; i++){
			letters[i].update();
		}
		
		if(needsUpdate){
			int numSpacesNeeded = 10 - inputText.length();
			try{
				if(!leftAlign){
					// RIGHT-ALIGNED TEXT

					for (int i = 0; i < numSpacesNeeded; i++){
						letters[i].setVal(36);
					}
					for (int i = 0; i < 10-numSpacesNeeded; i++){
						char c = inputText.charAt(i);
						letters[i+numSpacesNeeded].setVal((int)c);
						
						

					}


					
				} else {
					// LEFT-ALIGNED TEXT

					for (int i = 0; i < inputText.length(); i++){
						char c = inputText.charAt(i);
						letters[i].setVal((int)c);

					}
					for (int i = 0; i < numSpacesNeeded; i++){
						letters[i+inputText.length()].setVal(36);

					}

				}
			}
			catch(Exception e){
				e.printStackTrace();
				System.out.println("TextHandler did something wrong.");
				System.exit(0);
			}

			requireUpdate(false);
		}
	}

	public boolean isAlive(){
		return isAlive;
	}
	
	public Text getTextAt(int index){
		return letters[index];
	}

	public void setText(String text){
		inputText = text;
		requireUpdate(true);
	}
	
	public void makeAlive(boolean isIt){
		isAlive = isIt;
	}

	public int getX(){
		return (int)x;
	}

	public int getY(){
		return (int)y;
	}

	public void requireUpdate(boolean doesIt){
		needsUpdate = doesIt;
	}

	public int getLifetime() {
		return totalDuration - curDuration;
	}
}
